local jlog = require "config.logger"

-- 设置诊断符号
local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
for type, icon in pairs(signs) do
    local hl = "DiagnosticSign" .. type
    vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end

-- [帮助提示] --------------------------------------------
local function telescope_two_pane_selector(data)
    -- 确保 telescope 已加载并初始化
    local ok, telescope = pcall(require, "telescope")
    if not ok then
        vim.notify("Telescope not installed", vim.log.levels.ERROR)
        return
    end

    -- 确保加载必要的子模块
    local pickers = require "telescope.pickers"
    local finders = require "telescope.finders"
    local conf = require("telescope.config").values

    -- 创建自定义 picker
    pickers
        .new({
            prompt_title = "帮助提示",
            finder = finders.new_table {
                results = vim.tbl_keys(data),
                entry_maker = function(entry)
                    return {
                        value = entry,
                        display = entry,
                        ordinal = entry,
                    }
                end,
            },
            sorter = conf.generic_sorter {},
            previewer = require("telescope.previewers").new_buffer_previewer {
                title = "内容",
                define_preview = function(self, entry)
                    local sub_items = data[entry.value] or {}
                    local lines = { "# " .. entry.value, "" }
                    vim.list_extend(lines, sub_items)

                    vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
                end,
            },
            layout_config = {
                width = 0.9,
                height = 0.8,
                preview_width = 0.5, -- 右侧预览窗口宽度
                horizontal = {
                    preview_width = 0.6,
                },
            },
        })
        :find()
end
-- 定义二维数据
local items_data = {
    ["环绕 surround"] = {
        "添加环绕 ys{motion}{char}",
        "surr*ound_words             ysiw)           (surround_words)",
        '*make strings               ys$"            "make strings"',
        "",
        "删除 ds{char}",
        "[delete ar*ound me!]        ds]             delete around me!",
        "remove <b>HTML t*ags</b>    dst             remove HTML tags",
        "",
        "修改 cs{target}{replacement}",
        [['change quot*es'            cs'"            "change quotes"]],
        "<b>or tag* types</b>        csth1<CR>       <h1>or tag types</h1>",
        "delete(functi*on calls)     dsf             function calls",
    },
    ["变量跳转 RRethy/vim-illuminate"] = {
        "<a-n> 当前变量的下一个使用位置",
        "<a-p> 当前变量的上一个使用位置",
    },
}

-- 映射快捷键
vim.keymap.set("n", "<leader>hh", function()
    telescope_two_pane_selector(items_data)
end, { desc = "Open two-pane Telescope selector" })

------------------------------------------------------
local function has_boxes()
    return vim.fn.executable "boxes" == 1
end
local function simple_command_executor()
    if not has_boxes() then
        jlog.log_error "未找到 boxes 命令，请先安装"
        return
    end
    local commands = {
        { cmd = "boxes -d bear", desc = "小熊举牌" },
        { cmd = "boxes -d columns", desc = "两端蜡烛" },
        { cmd = "boxes -d cowsay", desc = "老牛说" },
        { cmd = "boxes -d ian_jones", desc = "六毛的大脚丫" },
        { cmd = "boxes -d jstone", desc = "通用注释" },
        { cmd = "boxes -d parchment", desc = "法典" },
        { cmd = "boxes -d stone", desc = "矩形框" },
        { cmd = "boxes -d tux", desc = "linux logo say" },
        { cmd = "boxes -d whirly", desc = "折线包围" },
    }

    vim.ui.select(commands, {
        prompt = "选择要执行的命令:",
        format_item = function(item)
            return string.format("%-20s - %s", item.cmd, item.desc)
        end,
    }, function(choice)
        if not choice then
            return
        end
        vim.ui.input({ prompt = "输入参数 (直接回车跳过): " }, function(input)
            if not input then
                return
            end
            local cmd = choice.cmd
            if input and #input > 0 then
                cmd = 'echo "' .. input .. '" | fold -s -w 80 | ' .. cmd
            else
                cmd = 'echo "" | ' .. cmd
            end
            local ok, output = pcall(vim.fn.systemlist, cmd)
            if not ok then
                jlog.log_error("执行命令失败: " .. output)
                return
            end
            vim.api.nvim_put(output, "l", true, true)
        end)
    end)
end

vim.keymap.set("n", "<leader>sc", simple_command_executor, { desc = "简单命令执行器" })
